package com.bootdo.act.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.act.domain.ActDetailsDO;
import com.bootdo.act.service.ActDetailsService;
import com.bootdo.blog.domain.ContentDO;
import com.bootdo.blog.service.ContentService;
import com.bootdo.common.controller.BaseController;
import com.bootdo.common.domain.Tree;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;
import com.bootdo.system.domain.DeptDO;
import com.bootdo.system.domain.UserDO;

/**
 * 测试
 * 
 * @author kewenxiu
 * @email 904358838@qq.com
 * @date 2017-11-23 16:37:40
 */
 
@Controller
@RequestMapping("/act/actDetails")
public class ActDetailsController extends BaseController{
	@Autowired
	private ActDetailsService actDetailsService;
	
	@Autowired
	private ContentService bContentService;
	
	@GetMapping()
	@RequiresPermissions("act:actDetails:actDetails")
	String ActDetails(){
	    return "act/actDetails/actDetails";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("act:actDetails:actDetails")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<ActDetailsDO> actDetailsList = actDetailsService.list(query);
		int total = actDetailsService.count(query);
		PageUtils pageUtils = new PageUtils(actDetailsList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("act:actDetails:add")
	String add(){
	    return "act/actDetails/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("act:actDetails:edit")
	String edit(@PathVariable("id") Long id,Model model){
		ActDetailsDO actDetails = actDetailsService.get(id);
		actDetails.setBlogList(getBlogList(id));
		model.addAttribute("actDetails", actDetails);
	    return "act/actDetails/edit";
	}
	
	@GetMapping("/read/{id}")
	String read(@PathVariable("id") Long id,Model model){
		ActDetailsDO actDetails = actDetailsService.get(id);
		actDetails.setBlogList(getBlogList(id));
		model.addAttribute("actDetails", actDetails);
	    return "act/actDetails/edit";
	}
	
	private List<ContentDO> getBlogList(Long actDetailsId){
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("actDetailsId", actDetailsId);
		return bContentService.list(map);
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("act:actDetails:add")
	public R save( ActDetailsDO actDetails){
		actDetails.setCreaterId(getUserId());
		actDetails.setCreaterName(getUsername());
		actDetails.setCreateDate(new Date());
		if(actDetailsService.save(actDetails)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("act:actDetails:edit")
	public R update( ActDetailsDO actDetails){
		actDetails.setUpdateDate(new Date());
		actDetailsService.update(actDetails);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("act:actDetails:remove")
	public R remove( Long id){
		if(actDetailsService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("act:actDetails:batchRemove")
	public R remove(@RequestParam("ids[]") Long[] ids){
		actDetailsService.batchRemove(ids);
		return R.ok();
	}
	
	@GetMapping("/tree")
	@ResponseBody
	public Tree<ActDetailsDO> tree() {
		Tree<ActDetailsDO> tree = new Tree<ActDetailsDO>();
		tree = actDetailsService.getTree();
		return tree;
	}
	
	@GetMapping("/treeView")
	String treeView() {
		return  "act/actDetails/actDetailsTree";
	}
	
}
